package com.six.elemo_manger.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.avos.avoscloud.AVCloudQueryResult;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.CloudQueryCallback;
import com.avos.avoscloud.SaveCallback;
import com.six.elemo_manger.R;
import com.six.elemo_manger.adapter.MangerAdapter;
import com.six.elemo_manger.bean.FoodList;

import java.util.ArrayList;
import java.util.List;

public class Fra_manger extends Fragment {
    private android.support.v4.widget.SwipeRefreshLayout refreshLayout;
    private List<AVObject> list = new ArrayList<>();
    private MangerAdapter adapter;
    private ListView lv;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.layout_manger,container,false);
        lv=view.findViewById(R.id.lv_manger);
        refreshLayout=view.findViewById(R.id.refresh1);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        refreshLayout.setRefreshing(true);
        get();
        adapter = new MangerAdapter(list,getContext());
        lv.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                get();
            }
        });

    }
    void get(){
        String cql = "select * from FoodList where food_isPay = 1";
        AVQuery.doCloudQueryInBackground(cql, new CloudQueryCallback<AVCloudQueryResult>() {
            @Override
            public void done(AVCloudQueryResult avCloudQueryResult, AVException e) {
                if (e==null){
                    list = (List<AVObject>) avCloudQueryResult.getResults();
                    adapter = new MangerAdapter(list,getContext());
                    lv.setAdapter(adapter);
                    refreshLayout.setRefreshing(false);

                }else {
                    Log.d("paye",e.toString()+"");
                }

            }
        });


    }
    public void toast(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }
/*
 //执行发货
                toast(i+"");
                // 第一参数是 className,第二个参数是 objectId
                final AVObject todo = AVObject.createWithoutData("FoodList", list.get(i).getObjectId());
                // 修改 content
                todo.put("food_isPay",0);
                // 保存到云端
                todo.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e==null){
                            Toast.makeText(getContext(), "发货成功", Toast.LENGTH_SHORT).show();
                              get();
                        }else {
                            Toast.makeText(getContext(), "发货失败", Toast.LENGTH_SHORT).show();

                        }
                    }
                });
*/
}
